﻿#!/usr/bin/env python
#coding:utf-8
from probe.core.level import *
from commands import *
import datetime
import time
def timeerror(args):
    '''监控系统时间误差'''
    day = time.strftime('%Y,%m,%d',time.localtime()).split(',')
    ltime = time.ctime().split()[3].split(':')
    result = getstatusoutput("rdate -p %s|awk '{print $6}'|head -1"%args.get('Timehost'))
    if result[0] != 0:
        return WarningData({'检测时间误差失败': [int(result[0]), 'Error']})
    rtime = result[1].split(':')
    localtime = datetime.datetime(int(day[0]),int(day[1]),int(day[2]),int(ltime[0]),int(ltime[1]),int(ltime[2]))
    remotetime = datetime.datetime(int(day[0]),int(day[1]),int(day[2]),int(rtime[0]),int(rtime[1]),int(rtime[2]))
    num1 = (remotetime-localtime).seconds
    num2 = (localtime-remotetime).seconds
    if num1 <= num2:
        errornum = num1
    else:
        errornum = num2
    if errornum >= args.get('WarningData'):
        return WarningData({'系统时间误差': [int(errornum), '秒']})
    return InfoData({'系统时间误差': [int(errornum), '秒']})
   
if __name__ == '__main__':
    print timeerror({'Timehost':'10.1.1.58','WarningData':120})
